<?
/**
 * iphone.php
 * 
 * Functions calls for the Iphone App
 * These are simple calls to alow the iPhone to
 * interact with the database
 */


$browser = strpos($_SERVER['HTTP_USER_AGENT'],"ymposium");
if ($browser == false) {
	if(isset($_GET['scavengerresponce'])) {
		include("scavenger.php");
		
	} else{
		include("userinfo.php");
	}
} else {

include("include/database.php");
/* The following to to extract the methods parameters from the POST data */
if (isset ($_GET['id'])) {  
        $usId = $_GET['id'];
        $username = $database->id2username($usId);
}

if (isset ($_GET['user']))  {  
        $username = trim($_GET['user']);
        $usId = $_GET['user'];
        $username = $database->id2username($usId);
}

if (isset ($_GET['pass']))  {  
        $password = trim($_GET['pass']);
}

if (isset ($_GET['clue']))  {  
        $clue = trim($_GET['clue']);
}

if (isset ($_GET['venderid'])) {  
        $venId = $_GET['venderid'];
        $venderName = $database->id2username($venId);
}



/**
 * Mark Contact between two people and assigns points
 *  Returns none
 */
function madeContact($username,$req_user ) {
	global $database;
	$userlevel = $database->getUserLevel($username);
	if(!$database->userHasScanedUser($username, $req_user) && strcmp($username, $req_user) != 0){

		$database->userScanUser($username, $req_user);
		// deal with adding points
		if($userlevel == USER_LEVEL ) { // Visitor scans someone
			$database->addUserPoints($username,VISITOR_SCAN_CREDIT);
			
		} elseif($userlevel == MERCHANT_LEVEL ) { // A vendor scan's someone
			$database->addUserPoints($req_user,MERCHANT_SCAN_CREDIT); // Give the cust pts
			$database->addUserPoints($username,VISITOR_SCAN_CREDIT);
			
		} elseif($userlevel == ADMIN_LEVEL ) { // one of the admins scaned 
			
		} else { // humm... shouldnt be here
			die("Got to location should have been");
		}
	}
	return array(   );
}

/* Returns YES / NO with the string for the clue also mark down scan */
function scavengerResponce($username, $clue) {
	global $database;
	$userinfo = $database->getUserInfo($username);
	$previousClueNum = $database->previousScannedUser($username);
	$clueInf = $database->scavangerClue($clue);
	$wrongHint = $database->getWrongHint($previousClueNum);
	if($clue <>($previousClueNum+1)) {
		  return array( "YES"  , "$wrongHint"  );
	}
        $database->addUserPoints($username,10);
	$database->userScanUser($username, "scavenger".$clue);
	return array( "YES", "$clueInf"  );
}
/**
 * getUserPoints - returns the amount of points user has.
 */    
function getPoints($username) {
	global $database;
	$userpts = $database->getUserPoints($username);
	return array( intval($userpts)  );
}

/**
 * userLogin - Checks whether or not the given
 * username is in the database, if so it checks if the
 * given password is the same password in the database
 * for that user. If the user doesn't exist or if the
 * passwords don't match up, it returns an array with 
 * BAD REASON, on success, returns GOOD
 */
function userLogin($username, $password){
	global $database;
	
	if(!eregi("^([0-9a-z])*$", $username) || !$username){
		return array( "BAD USERNAME" );
	} 
	
	if(!$password){
		return array( "BAD PASSWORD" );		
	}
	
	$userin = $database->confirmUserPass(stripslashes($username), md5($password));
	
	if($userin == 0) { // Username and password match
		return array( "GOOD" );
		
	} elseif($userin == 1) { // Bad Username
		return array( "BAD USERNAME" );
	
	} elseif($userin == 2) { // Bad Password
		return array( "BAD PASSWORD" );
	
	} else { // unknown fail
		return array( "UNK ERROR" );
	}
}

/**
 * getUserId - Returns the user id associated 
 * with the username. If not in the database, return 0.
 */
function getUserId($username){
	global $database;
	$userinfo = $database->getUserInfo($username);
	return array( intval($userinfo['id'])  );
}


function enterRace($username) {
	global $database;
	$userinfo = $database->getUserInfo($username);
	return array(   );
}

/**
 * getContactInfo - Returns a in a list format the
 * first name, last name, organization, website, 
 * e-mail, phone, and address of the person.
 */
function getContactInfo($username) {
	global $database;
	$userinfo = $database->getUserInfo($username);
	return array($userinfo['fname'], $userinfo['lname'], $userinfo['organization'], $userinfo['website'], $userinfo['email'], $userinfo['phone'], $userinfo['address']  );
}


// The returning object is done here!.
if(      isset($_GET['madecontact'])) {
	echo json_encode(madeContact($username, $venderName));
	
} elseif(isset($_GET['scavengerresponce'])) {
	echo json_encode(scavengerResponce($username, $clue));
	
} elseif(isset($_GET['getpoints'])) {
	echo json_encode(getPoints($username));
	
} elseif(isset($_GET['userlogin'])) {
        $username = trim($_GET['user']);
	echo json_encode(userLogin($username, $password));
	
} elseif(isset($_GET['getuserid'])) {
        $username = trim($_GET['user']);
	echo json_encode(getUserId($username));
	
} elseif(isset($_GET['enterrace'])) {
	echo json_encode(enterRace($username));
	
} elseif(isset($_GET['getcontact'])) {
	echo json_encode(getContactInfo($username));
	
} 

} # end else
?>
